package ch.fhnw.qualiscan.core.libraries;

import ch.fhnw.qualiscan.core.db.pojos.Language;
import eu.livotov.tpt.TPTApplication;
import eu.livotov.tpt.i18n.TM;
import java.io.Serializable;
import java.util.Locale;

/**
 * This library takes care of loading/unloading and locating languages.
 * @author benjamin groehbiel
 *
 */
@SuppressWarnings("serial")
public class LanguageLibrary implements Serializable {

	private Language currentLanguage;
	
	public Language getCurrentLanguage() {
		return currentLanguage;
	}
	
        public Language getDefaultLanguage() {
            Language language = new Language();
            language.setId(2);
            language.setLanguage("English");
            language.setLocale("en");
            return language;
        }
        
	public void setLanguage(String lang) {
		
		// http://download.oracle.com/javase/1.4.2/docs/guide/intl/locale.doc.html
		Locale currentLocale;
		
		if(lang.isEmpty()) {
			currentLocale = new Locale("en_US");
			TPTApplication.getCurrentApplication().setLocale(currentLocale);
			TM.setDefaultLanguage("en");
			
			/*
			Locale locale = TPTApplication.getCurrentApplication().getLocale();
			String language = locale.getLanguage();
			currentLocale = new Locale("en_US");
			
			
			LanguageModel langModel = new LanguageModel();
			if(langModel.getLanguage(language)) {
				TPTApplication.getCurrentApplication().setLocale(currentLocale);
				TM.setDefaultLanguage(language);
			} else {
				TPTApplication.getCurrentApplication().setLocale(currentLocale);
				TM.setDefaultLanguage("en");
			}*/
		} else if(lang.equals("de")) {
			currentLocale = new Locale("de_DE");
			TPTApplication.getCurrentApplication().setLocale(currentLocale);
			TM.setDefaultLanguage("de");
		} 
		else if(lang.equals("it")) {
			currentLocale = new Locale("it_IT");
			TPTApplication.getCurrentApplication().setLocale(currentLocale);
			TM.setDefaultLanguage("it");
		} else if(lang.equals("en")) {
			currentLocale = new Locale("en_US");
			TPTApplication.getCurrentApplication().setLocale(currentLocale);
			TM.setDefaultLanguage("en");
		} 
		else if(lang.equals("fr")) {
			currentLocale = new Locale("fr_FR");
			TPTApplication.getCurrentApplication().setLocale(currentLocale);
			TM.setDefaultLanguage("fr");
		}
		else if(lang.equals("it")) {
			currentLocale = new Locale("it_IT");
			TPTApplication.getCurrentApplication().setLocale(currentLocale);
			TM.setDefaultLanguage("it");
		}
		else {
			currentLocale = new Locale("en_US");
			TPTApplication.getCurrentApplication().setLocale(currentLocale);
			TM.setDefaultLanguage("en");
		}
		
		java.util.Locale.setDefault(currentLocale);
		
		//System.out.println("TM lang" + TM.getDictionary().getDefaultLanguage() + " Locale " + TPTApplication.getCurrentApplication().getLocale().getLanguage());
	}
	
}
